﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Model;
using BF.Infrastructure;
using BF.IBLL.BaseSystem.AreaManagement;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;

namespace BF.Web.pages.UserControls
{
    public partial class SysAreaControl : System.Web.UI.UserControl
    {
        #region 获得当前地区编码

        WSJD_BaseFrameworkEntities entity = new WSJD_BaseFrameworkEntities();
        /// <summary>
        /// 当前选中的地区编码
        /// </summary>
        public String AreaCode
        {
            get
            {
                return GetAreaCode();
            }
        }
        /// <summary>
        /// 获取当前选中的省的编码
        /// </summary>
        public string provinceCode
        {
            get
            {
                return this.sProvince.SelectedValue;
            }
        }



        /// <summary>
        /// 获取当前选中的省的名称
        /// </summary>
        public string provinceName
        {
            get
            {
                return this.sProvince.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 获取当前选中的市的编码
        /// </summary>
        public string cityCode
        {
            get
            {
                return this.sCity.SelectedValue;
            }
        }

        /// <summary>
        /// 获取当前选中的市的名称
        /// </summary>
        public string cityName
        {
            get
            {
                return this.sCity.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 获取当前选中的县的编码
        /// </summary>
        public string countryCode
        {
            get
            {
                return this.sCounty.SelectedValue;
            }
        }

        /// <summary>
        /// 获取当前选中的县的名称
        /// </summary>
        public string countryName
        {
            get
            {
                return this.sCounty.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 获取当前选中的乡镇的编码
        /// </summary>
        public string townCode
        {
            get
            {
                return this.sTwon.SelectedValue;
            }
        }
        /// <summary>
        /// 获取当前选中的乡镇的编码
        /// </summary>
        public string townName
        {
            get
            {
                return this.sTwon.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 获取当前选中的村的编码
        /// </summary>
        public string villageCode
        {
            get
            {
                return this.sVillage.SelectedValue;
            }
        }
        /// <summary>
        /// 获取当前选中的村的名称
        /// </summary>
        public string villageName
        {
            get
            {
                return this.sVillage.SelectedItem.Text;
            }
        }
        /// <summary>
        /// 设置是否根据用户类型enbale相关控件，【比如市级用户进入系统，控制省、市不可选择】
        /// </summary>
        public bool EnableControlsByUserType
        {
            get
            {
                return ViewState["EnableControlsByUserType"] == null ? true : (bool)ViewState["EnableControlsByUserType"];
            }
            set
            {
                ViewState["EnableControlsByUserType"] = value;
            }

        }
        /// <summary>
        /// 禁用所有的控件
        /// </summary>
        public bool EnableAll
        {
            get
            {
                return ViewState["EnableAll"] == null ? false : (bool)ViewState["EnableAll"];
            }
            set
            {
                ViewState["EnableAll"] = value;
            }
        }

        #endregion

        #region 页面生命周期
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.MyDataBind("000000");
                if (EnableAll)//禁用所有控件
                {
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = false;
                    this.sCounty.Enabled = false;
                    this.sTwon.Enabled = false;
                    this.sVillage.Enabled = false;
                }

            }
        }
        #endregion

        #region 数据初始绑定
        /// <summary>
        /// 数据初始绑定
        /// </summary>
        /// <param name="superID"></param>
        protected void MyDataBind(string superID)
        {
            this.sProvince.Items.Clear();
            this.sProvince.Items.Add(new ListItem("--请选择省--", "-1"));
            this.sProvince.DataSource = this.GetSysArea(superID);
            this.sProvince.DataBind();
        }
        #endregion

        /// <summary>
        /// 根据上级地区编码获取地区数据
        /// </summary>
        /// <param name="superID">上级地区编码</param>
        /// <returns></returns>
        protected List<sys_Area> GetSysArea(string superID)
        {
            List<sys_Area> sysareaList = new List<sys_Area>();
            var query = from p in entity.sys_Area
                        where p.sSuperID == superID
                        select p;
            sysareaList.AddRange(query.ToList<sys_Area>());
            return sysareaList;
        }
        #region 页面控件的事件
        /// <summary>
        /// 省下拉事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sCity.Items.Clear();
            this.sCity.Items.Add(new ListItem("--请选择市--", "-1"));
            this.sCounty.Items.Clear();
            this.sCounty.Items.Add(new ListItem("--请选择县--", "-1"));
            this.sTwon.Items.Clear();
            this.sTwon.Items.Add(new ListItem("--请选择乡镇--", "-1"));
            this.sVillage.Items.Clear();
            this.sVillage.Items.Add(new ListItem("--请选择村--", "-1"));
            this.sCity.DataSource = this.GetSysArea(this.sProvince.SelectedValue);
            this.sCity.DataBind();
        }
        /// <summary>
        /// 城市下拉事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sCounty.Items.Clear();
            this.sCounty.Items.Add(new ListItem("--请选择县--", "-1"));
            this.sTwon.Items.Clear();
            this.sTwon.Items.Add(new ListItem("--请选择乡镇--", "-1"));
            this.sVillage.Items.Clear();
            this.sVillage.Items.Add(new ListItem("--请选择村--", "-1"));
            this.sCounty.DataSource = this.GetSysArea(this.sCity.SelectedValue);
            this.sCounty.DataBind();
        }
        /// <summary>
        /// 县下拉事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sCounty_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sTwon.Items.Clear();
            this.sTwon.Items.Add(new ListItem("--请选择乡镇--", "-1"));
            this.sVillage.Items.Clear();
            this.sVillage.Items.Add(new ListItem("--请选择村--", "-1"));
            this.sTwon.DataSource = this.GetSysArea(this.sCounty.SelectedValue);
            this.sTwon.DataBind();
        }
        /// <summary>
        /// 乡镇下拉事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sTwon_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sVillage.Items.Clear();
            this.sVillage.Items.Add(new ListItem("--请选择村-", "-1"));
            this.sVillage.DataSource = this.GetSysArea(this.sTwon.SelectedValue);
            this.sVillage.DataBind();
        }
        #endregion


        #region 页面方法
        /// <summary>
        /// 获取当前选择的地区编码
        /// </summary>
        /// <returns></returns>
        protected string GetAreaCode()
        {
            string areaCode = string.Empty;
            if (this.sVillage.SelectedIndex > 0)
            {
                areaCode = this.sVillage.SelectedValue;
            }
            else if (this.sTwon.SelectedIndex > 0)
            {
                areaCode = this.sTwon.SelectedValue;
            }
            else if (this.sCounty.SelectedIndex > 0)
            {
                areaCode = this.sCounty.SelectedValue;
            }
            else if (this.sCity.SelectedIndex > 0)
            {
                areaCode = this.sCity.SelectedValue;
            }
            else if (this.sProvince.SelectedIndex > 0)
            {
                areaCode = this.sProvince.SelectedValue;
            }

            return areaCode;
        }

        /// <summary>
        /// 设置控件禁用
        /// </summary>
        /// <param name="level">9全部启用;5:省级、市级、县区级、镇级、村级禁用;4：省级、市级、县区级、镇级禁用;3：省级、市级、县区级禁用;2:省级、市级禁用;1：省级禁用</param>
        public void SetEnabledControl(int level)
        {
            switch (level)
            {
                case 1:
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = true;
                    this.sCounty.Enabled = true;
                    this.sTwon.Enabled = true;
                    this.sVillage.Enabled = true;
                    break;
                case 2:
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = false;
                    this.sCounty.Enabled = true;
                    this.sTwon.Enabled = true;
                    this.sVillage.Enabled = true;
                    break;
                case 3:
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = false;
                    this.sCounty.Enabled = false;
                    this.sTwon.Enabled = true;
                    this.sVillage.Enabled = true;
                    break;
                case 4:
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = false;
                    this.sCounty.Enabled = false;
                    this.sTwon.Enabled = false;
                    this.sVillage.Enabled = true;
                    break;
                case 5:
                    this.sProvince.Enabled = false;
                    this.sCity.Enabled = false;
                    this.sCounty.Enabled = false;
                    this.sTwon.Enabled = false;
                    this.sVillage.Enabled = false;
                    break;
                default:
                    this.sProvince.Enabled = true;
                    this.sCity.Enabled = true;
                    this.sCounty.Enabled = true;
                    this.sTwon.Enabled = true;
                    this.sVillage.Enabled = true;
                    return;
            }
        }

        /// <summary>
        /// 初始化用户控件,根据地区禁用下拉框
        /// </summary>
        /// <param name="provinceCode"></param>
        /// <param name="cityCode"></param>
        /// <param name="countyCode"></param>
        public void SetSysAreaValue_EnabledTrue(string provinceCode, string cityCode, string countyCode, string townCode, string cunCode)
        {
            if (!string.IsNullOrEmpty(provinceCode))
            {
                this.MyDataBind("000000");
                this.sProvince.SelectedValue = provinceCode;
                this.sProvince_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = provinceCode;
                this.sProvince.Enabled = !EnableControlsByUserType;
            }
            if (!string.IsNullOrEmpty(cityCode))
            {
                this.sCity.SelectedValue = cityCode;
                this.sCity_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = cityCode;
                this.sCity.Enabled = !EnableControlsByUserType;
            }
            if (!string.IsNullOrEmpty(countyCode))
            {
                this.sCounty.SelectedValue = countyCode;
                this.sCounty_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = countyCode;
                this.sCounty.Enabled = !EnableControlsByUserType;
            }
            if (!string.IsNullOrEmpty(townCode))
            {
                this.sTwon.SelectedValue = townCode;
                this.sTwon_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = townCode;
                this.sTwon.Enabled = !EnableControlsByUserType;
            }
            if (!string.IsNullOrEmpty(cunCode))
            {
                this.sVillage.SelectedValue = cunCode;
                this.hid_ddlareaCode.Value = cunCode;
                this.sVillage.Enabled = !EnableControlsByUserType;
            }
        }

        /// <summary>
        /// 初始化用户控件，不禁用下拉框
        /// </summary>
        /// <param name="provinceCode"></param>
        /// <param name="cityCode"></param>
        /// <param name="countyCode"></param>
        /// <param name="townCode"></param>
        /// <param name="cunCode"></param>
        public void SetSysAreaValue_EnabledFalse(string provinceCode, string cityCode, string countyCode, string townCode, string cunCode)
        {
            if (!string.IsNullOrEmpty(provinceCode))
            {
                this.MyDataBind("000000");
                this.sProvince.SelectedValue = provinceCode;
                this.sProvince_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = provinceCode;
            }
            if (!string.IsNullOrEmpty(cityCode))
            {
                this.sCity.SelectedValue = cityCode;
                this.sCity_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = cityCode;
            }
            if (!string.IsNullOrEmpty(countyCode))
            {
                this.sCounty.SelectedValue = countyCode;
                this.sCounty_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = countyCode;
            }
            if (!string.IsNullOrEmpty(townCode))
            {
                this.sTwon.SelectedValue = townCode;
                this.sTwon_SelectedIndexChanged(null, null);
                this.hid_ddlareaCode.Value = townCode;
            }
            if (!string.IsNullOrEmpty(cunCode))
            {
                this.sVillage.SelectedValue = cunCode;
                this.hid_ddlareaCode.Value = cunCode;
            }
        }

        /// <summary>
        /// 根据等级设置隐藏
        /// </summary>
        /// <param name="level">5:村级隐藏;4:镇、村隐藏;3:县、镇、村隐藏;2:市、县、镇、村隐藏、1：全部隐藏</param>
        public void SetHideControl(int level)
        {
            switch (level)
            {
                case 1:
                    this.sProvince.Visible = false;
                    this.sCity.Visible = false;
                    this.sCounty.Visible = false;
                    this.sTwon.Visible = false;
                    this.sVillage.Visible = false;
                    break;
                case 2:
                    this.sCity.Visible = false;
                    this.sCounty.Visible = false;
                    this.sTwon.Visible = false;
                    this.sVillage.Visible = false;
                    break;
                case 3:
                    this.sCounty.Visible = false;
                    this.sTwon.Visible = false;
                    this.sVillage.Visible = false;
                    break;
                case 4:
                    this.sTwon.Visible = false;
                    this.sVillage.Visible = false;
                    break;
                case 5:
                    this.sVillage.Visible = false;
                    break;
                default:
                    return;
            }
        }
        #endregion

        /// <summary>
        /// 根据地区获得等级
        /// </summary>
        /// <param name="areaCode"></param>
        public int GetLevelByAreaCode(string areaCode)
        {
            Isys_AreaBLL iBLL = IOCFactory.GetIOCResolve<Isys_AreaBLL>();
            return iBLL.GetLevelByAreaCode(areaCode);
        }

        /// <summary>
        /// 根据一个地区编码，智能反绑地区控件
        /// </summary>
        /// <param name="areaCode"></param>
        public void SetSysAreaValue(string areaCode)
        {
            //if (!string.IsNullOrEmpty(areaCode))//需要对控件进行设置
            //{
            //    SetEnabledBindControl(areaCode);
            //}
            //else
            //{
            //    areaCode = "000000";
            //    SetNoEnabledBindControl(areaCode);
            //}
            SetEnabledBindControl(areaCode);
        }

        /// <summary>
        /// 初始化智能绑数据需要禁用控件的方法
        /// </summary>
        private void SetEnabledBindControl(string areaCode)
        {
            Isys_AreaBLL iBLL = IOCFactory.GetIOCResolve<Isys_AreaBLL>();
            string provinceCodeString = string.Empty;
            string cityCodeString = string.Empty;
            string countyCodeString = string.Empty;
            string townCodeString = string.Empty;
            string villageCodeString = string.Empty;
            int level = 0;
            string subAreaCode = iBLL.GetSubAreaCode(areaCode, out level).ToString();
            if (level == 1)//省级
            {
                provinceCodeString = areaCode;
            }
            if (level == 2)//市级
            {
                cityCodeString = areaCode;

                provinceCodeString = iBLL.GetModelByID(areaCode).sSuperID;
            }
            if (level == 3)//县级
            {
                countyCodeString = areaCode;
                cityCodeString = iBLL.GetModelByID(areaCode).sSuperID;
                provinceCodeString = iBLL.GetModelByID(cityCodeString).sSuperID;
            }
            if (level == 4)//乡镇级
            {
                townCodeString = areaCode;
                countyCodeString = iBLL.GetModelByID(areaCode).sSuperID;
                cityCodeString = iBLL.GetModelByID(countyCodeString).sSuperID;
                provinceCodeString = iBLL.GetModelByID(cityCodeString).sSuperID;
            }
            if (level == 5)//村级
            {
                villageCodeString = areaCode;
                townCodeString = iBLL.GetModelByID(areaCode).sSuperID;
                countyCodeString = iBLL.GetModelByID(townCodeString).sSuperID;
                cityCodeString = iBLL.GetModelByID(countyCodeString).sSuperID;
                provinceCodeString = iBLL.GetModelByID(cityCodeString).sSuperID;
            }
            this.SetSysAreaValue_EnabledTrue(provinceCodeString, cityCodeString, countyCodeString, townCodeString, villageCodeString);


        }

        /// <summary>
        /// 初始化只能绑数据不禁用控件的方法
        /// </summary>
        private void SetNoEnabledBindControl(string areaCode)
        {
            MyDataBind(areaCode);
        }


    }
}